"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "FKD" + formatDate(new Date(), "yyyyMMdd");

describe("visitRecord", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
        // 创建一个职工
        var result = httpPost("POST", "/v1.0/api/user/Create", {
            "job_number": "001",
            "name": "测试职工一",
            "gender": "男",
            "phone": "18300000002",
            "role": "职工",
            "region": "所在区域",
            "image": "",
            "company": "公司",
            "position": "职务",
        })

    });

    after(function() {
        clearDB();
    });


    it("发起预约", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Create", {
            "visitors": [{
                "visitor_name": "我是访客",
                "visitor_id_number": "123456789012345679",
            }],
            "receptionist_phone": "18888888888",
            "visit_time": "2099-12-01 11:00",
            "departure_time": "2099-12-01 10:00",
            "visit_reason": "参观",
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "离开时间必须大于拜访时间");

        result = httpPost("POST", "/v1.0/api/visitRecord/Create", {
            "visitors": [{
                "visitor_name": "我是访客",
                "visitor_id_number": "123456789012345679",
            }],
            "receptionist_phone": "18888888888",
            "visit_time": "2099-11-01 11:00",
            "departure_time": "2099-12-01 12:11",
            "visit_reason": "参观",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("预约详情", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.no, str_prefix + "001");
        assert.equal(result.data.visitors.length, 1);
        assert.equal(result.data.visitor_name, "企业管理员");
        assert.equal(result.data.visitor_gender, "男");
        assert.equal(result.data.visitor_id_number, "");
        assert.equal(result.data.visitor_image, "c6a2645c3d89653dd7006d3eb3789f884336596c");
        assert.equal(result.data.visitor_phone, "18888888888");
        assert.equal(result.data.visitors[0].visitor_name, "我是访客");
        assert.equal(result.data.visitors[0].visitor_id_number, "123456789012345679");
        assert.equal(result.data.receptionist_id, 1);
        assert.equal(result.data.receptionist_name, "企业管理员");
        assert.equal(result.data.receptionist_image, "846482e6c31637e4d3ce374138aee0354137c78c");
        assert.equal(result.data.created_by, 1);
        assert.equal(result.data.created_name, "企业管理员");
        assert.equal(result.data.status, "待审核");
        assert.equal(result.data.vehicle_number, 0);
        assert.equal(result.data.visitor_number, 2);
        // assert.equal(result.data.visit_time, "11-01 11:00");
        // assert.equal(result.data.departure_time, "20天前 12:11");
        assert.equal(result.data.code, undefined);
    });

    it("编辑预约", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Update", {
            "id": 1,
            "visitors": [],
            "receptionist_phone": "18300000002",
            "visit_time": "2099-11-01 12:00",
            "departure_time": "2099-12-01 13:11",
            "visit_reason": "测试",
        })
        assert.equal(result.code, "200");

        var result = httpPost("POST", "/v1.0/api/visitRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.visitors.length, 0);
        assert.equal(result.data.receptionist_id, 2);
        assert.equal(result.data.receptionist_name, "测试职工一");
        assert.equal(result.data.receptionist_image, "846482e6c31637e4d3ce374138aee0354137c78c");
        assert.equal(result.data.created_by, 1);
        assert.equal(result.data.created_name, "企业管理员");
        assert.equal(result.data.status, "待审核");
        assert.equal(result.data.vehicle_number, 0);
        assert.equal(result.data.visitor_number, 1);
        // assert.equal(result.data.visit_time, "11-01 12:00");
        // assert.equal(result.data.departure_time, "22天前 13:11");

        result = httpPost("POST", "/v1.0/api/visitRecord/Update", {
            "id": 1,
            "visitors": [{
                "visitor_name": "我是访客",
                "visitor_id_number": "123456789012345679",
            }],
            "receptionist_phone": "18888888888",
            "visit_time": "2099-11-01 11:00",
            "departure_time": "2099-12-01 12:11",
            "visit_reason": "参观",
        })
        assert.equal(result.code, "200");
    })

    it("预约通过", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Pass", {
            "id": 1
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result = httpPost("POST", "/v1.0/api/visitRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.status, "已通过");
        assert.equal(result.data.reviewed_by, 1);
        assert.equal(result.data.code.substring(0, 21), "data:image/png;base64");
    });

    it("预约通过后，无法编辑", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Update", {
            "id": 1
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "访客预约已通过,不可再次编辑");
    })

    it("访客登陆：sync->registe", function() {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
        var result = httpPost("GET", "/v1.0/api/user/Sync?code=thisistestcode", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 0);
        var result = httpPost("POST", "/v1.0/api/user/Registe", {
            "name": "测试访客1",
            "gender": "男",
            "phone": "18300000001",
            "id_number": "123456789012345678",
            "image": "",
            "company": "公司",
            "position": "职务",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 3);
    });

    it("访客-发起预约-职工为接待人", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Create", {
            "visitors": [{
                "visitor_name": "我是主访人",
                "visitor_gender": "男",
                "visitor_id_number": "123456789012345678",
                "visitor_phone": "18300000001",
            }, {}, {
                "visitor_name": "我是访客",
                "visitor_id_number": "123456789012345679",
            }],
            "receptionist_phone": "18300000002",
            "visit_time": "2099-12-01 11:00",
            "departure_time": "2099-12-01 12:11",
            "visit_reason": "参观",
            "vehicle_number": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);
    });

    it("访客-预约列表", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.total, 1);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].vehicle_number, 2);
        assert.equal(result.data.list[0].visitor_number, 3);
        assert.equal(result.data.list[0].visitor_company, "公司");
        assert.equal(result.data.list[0].visitor_position, "职务");
    });

    it("职工登陆：sync->h5login", function() {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
        var result = httpPost("GET", "/v1.0/api/user/Sync", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 0);
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "phone": "18300000002",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
            "role": "职工",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);
    });

    it("职工-预约列表", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.total, 1);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].no, str_prefix + "002");
    });

    it("申请人非本人预约预约，无法编辑", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Update", {
            "id": 2
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "无权进行此操作");
    })

    it("预约拒绝", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Refuse", {
            "id": 2
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result = httpPost("POST", "/v1.0/api/visitRecord/Get", {
            "id": 2,
        })
        assert.equal(result.data.status, "已拒绝");
        assert.equal(result.data.reviewed_by, 2);
    });

    it("管理员 h5 登陆：sync->h5login", function() {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
        var result = httpPost("GET", "/v1.0/api/user/Sync", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 0);
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
            "role": "职工",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    })

    it("管理员-h5-预约列表", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.total, 1);
        assert.equal(result.data.list[0].id, 1);
    });

    it("管理员-h5-预约统计", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Static", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 1);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 1);
        assert.equal(result.data.refused, 0);
    })

    it("管理员-web-预约列表", function() {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        var result = httpPost("POST", "/v1.0/api/visitRecord/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.total, 2);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[1].id, 1);
    })

    it("管理员-web-预约统计", function() {
        var result = httpPost("POST", "/v1.0/api/visitRecord/Static", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 2);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 1);
        assert.equal(result.data.refused, 1);
    })

    // it("过期预约-不可审核", function() {
    //     var result = httpPost("POST", "/v1.0/api/visitRecord/Create", {
    //         "visitors": [{
    //             "visitor_name": "我是访客",
    //             "visitor_id_number": "123456789012345679",
    //         }],
    //         "receptionist_phone": "18888888888",
    //         "visit_time": "2009-12-01 11:00",
    //         "departure_time": "2009-12-01 12:00",
    //         "visit_reason": "参观",
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 3);

    //     result = httpPost("POST", "/v1.0/api/visitRecord/Get", {
    //         "id": 3,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 3);
    //     assert.equal(result.data.status, "待审核");

    //     result = httpPost("POST", "/v1.0/api/visitRecord/Pass", {
    //         "id": 3
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "访客预约已过期");

    //     result = httpPost("POST", "/v1.0/api/visitRecord/Refuse", {
    //         "id": 3
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "访客预约已过期");
    // });
});

require('test').run();